package cn.daniellee.plugin.lm.core;

import cn.daniellee.plugin.lm.LotteryMaster;
import cn.daniellee.plugin.lm.model.PrizePool;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class LotteryCore {

    // 奖池锁定Map，使用奖池前统一进行检查，Key为奖池ID，Value为玩家ID
    public static Map<String, String> prizePoolLock = new ConcurrentHashMap<>();

    public static boolean isPrizePoolAvailable(String id) {
        return prizePoolLock.get(id) == null;
    }

    public static String whichPoolInUse(String playerName) {
        Map.Entry<String, String> entry = prizePoolLock.entrySet().stream().filter(item -> item.getValue().equals(playerName)).findFirst().orElse(null);
        return entry != null ? entry.getValue() : null;
    }

    public static List<PrizePool> getPrizePools(boolean fullAccess) {
        List<PrizePool> prizePools = LotteryMaster.getInstance().getStorage().getPrizePools();
        Bukkit.broadcastMessage("Debug: " + fullAccess);
        return fullAccess ? prizePools : prizePools.stream().filter(PrizePool::isOpen).collect(Collectors.toList());
    }

    public static PrizePool getPrizePoolById(String id) {
        List<PrizePool> prizePools = LotteryMaster.getInstance().getStorage().getPrizePools();
        return prizePools.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
    }

}
